package com.example.chenxiaojun.encrption;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

//非对称加密
//加密和解密两套密码
public class RSAActivity extends AppCompatActivity {
    TextView showKey1;
    TextView showKey2;
    TextView showinfo;
    PrivateKey privateKey;
    PublicKey publicKey;
    byte[] result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rsa);
        showKey1 = (TextView) findViewById(R.id.showKey1);
        showKey2 = (TextView) findViewById(R.id.showKey2);
        showinfo = (TextView) findViewById(R.id.showinfo);
    }

    public void decrypt(View view) {
        switch (view.getId()){
            case R.id.rsaDecrypt:
                try {
                    Cipher cipher=Cipher.getInstance("RSA");
                    if (publicKey!=null){
                        cipher.init(Cipher.DECRYPT_MODE,publicKey);
                        byte[] base64decode = Base64.decode(result, Base64.DEFAULT);
                        byte[] doFinal=cipher.doFinal(base64decode);
                        showinfo.setText("解密："+new String(doFinal));
                        Toast.makeText(RSAActivity.this, new String(doFinal), Toast.LENGTH_SHORT).show();
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
                break;
        }

    }
String check="1000000000000,123456";
    public void encrypt(View view) {
        switch (view.getId()){
            case R.id.rsaEncrypt:
                try {
                    Cipher cipher=Cipher.getInstance("RSA");
                    if (privateKey!=null){
                        cipher.init(Cipher.ENCRYPT_MODE,privateKey);
                        byte[] doFinal=cipher.doFinal(check.getBytes());
                        byte[] base64encode = Base64.encode(doFinal, Base64.DEFAULT);
                        result=base64encode;
                        showinfo.setText(check+","+new String(result));
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    public void generate(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
                    keyPairGenerator.initialize(1024);
                    KeyPair keyPair = keyPairGenerator.generateKeyPair();
                    privateKey = keyPair.getPrivate();
                    publicKey = keyPair.getPublic();

                    byte[] privateKeyEncoded = privateKey.getEncoded();
                    byte[] publicKeyEncoded = publicKey.getEncoded();

                    final byte[] privateKeyBase64Encode = Base64.encode(privateKeyEncoded, Base64.DEFAULT);
                    final byte[] publicKeyBase64Encoded = Base64.encode(publicKeyEncoded, Base64.DEFAULT);
                    showKey1.post(new Runnable() {
                        @Override
                        public void run() {
                            showKey1.setText("私钥：" + new String(privateKeyBase64Encode));
                        }
                    });
                    showKey2.post(new Runnable() {
                        @Override
                        public void run() {
                            showKey2.setText("公钥：" + new String(publicKeyBase64Encoded));
                        }
                    });

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
            }

        }).start();
    }
}
